#coding:utf-8
#from django.test import TestCase
from unittest import TestCase
from questioin.models import IType, IType_Major, Level, Question, Question_Char, Detail
from major.models import Major, Char

class IType_Test(TestCase):
    def create_itype(self):
        kwargs = {
            "author_id": 1,
            "name": "A1",
            "detail": "（单句型最佳选择题)",
        }
        self.itype = IType.create(kwargs)
        self.pk = self.itype.pk
        self.del_list.append(self.pk)
    def setUp(self):
        self.del_list = []
    def tearDown(self):
        IType.objects.filter(pk__in=self.del_list).delete()
    def test_relation_dict(self):
        test_dict = IType.relation_dict
        self.assertIn("is_hat", test_dict)
        self.assertIn("detail", test_dict)
    def test_create(self):
        #create
        kwargs = {
            "author_id": 1,
            "name": "A1",
            "is_hat": True,
            "detail": "（单句型最佳选择题)：每道试题由1个题干和5个供选择的备选答案组成。"
        }
        item = IType.create(kwargs)
        self.del_list.append(item.pk)
        self.assertEqual(item.author_id, 1)
        self.assertEqual(item.name, "A1")
        self.assertEqual(item.is_hat, True)
        self.assertEqual(item.detail, "（单句型最佳选择题)：每道试题由1个题干和5个供选择的备选答案组成。")
        #default
        kwargs = { "author_id": 1, }
        item2 = IType.create(kwargs)
        self.del_list.append(item2.pk)
        self.assertEqual(item2.is_hat, False)
    def test_update(self):
        self.create_itype()
        IType.update(self.pk, {
            "is_hat": False,
            "detail": u"(多选题)",
        })
        itype = IType.objects.get(pk=self.pk)
        self.assertEqual(itype.is_hat, False)
        self.assertEqual(itype.detail, u"(多选题)")
    def test_update(self):
        update_list = []
        self.create_itype()
        update_list.append(self.pk)
        self.create_itype()
        update_list.append(self.pk)
        itype_list = IType.search({"pk_list": update_list})
        self.assertEqual(itype_list.count(), 2)
        #search arg no use, except pk_list
        itype_list = IType.search({"name": "B"})
        self.assertNotEqual(itype_list.count(), 0)
class IType_Major_Test(TestCase):
    @classmethod
    def setUpClass(cls):
        cls.major_dele_list = []
        cls.major = Major.create({"author_id": 1,
                                  "name": "测试课程"})
        cls.major_dele_list.append(cls.major.pk)
        cls.itype_dele_list = []
        cls.itype_01 = IType.create({"author_id": 1,
                                     "name": "A1",})
        cls.itype_dele_list.append(cls.itype_01.pk)
        cls.itype_02 = IType.create({"author_id": 1,
                                     "name": "A2",})
        cls.itype_dele_list.append(cls.itype_02.pk)
        cls.itype_03 = IType.create({"author_id": 1,
                                     "name": "B1",})
        cls.itype_dele_list.append(cls.itype_03.pk)
        cls.itype_04 = IType.create({"author_id": 1,
                                     "name": "A3",})
        cls.itype_dele_list.append(cls.itype_04.pk)
        cls.itype_05 = IType.create({"author_id": 1,
                                     "name": "A4",})
        cls.itype_dele_list.append(cls.itype_05.pk)
    @classmethod
    def tearDownClass(cls):
        Major.objects.filter(pk__in=cls.major_dele_list).delete()
        IType.objects.filter(pk__in=cls.itype_dele_list).delete()
    def setUp(self):
        self.dele_list = []
    def tearDown(self):
        IType_Major.objects.filter(pk__in=self.dele_list).delete()
    def test_create(self):
        IType_Major.create(self.major.pk, self.itype_dele_list)
        item_list = IType_Major.objects.filter(major_id = self.major.pk)
        self.dele_list.extend([item.pk for item in item_list.iterator()])
        self.assertEqual(item_list.count(), 5)
        for item in item_list.iterator():
            self.assertEqual(item.major_id, self.major.pk)
            self.assertIn(item.itype_id, self.itype_dele_list)
    def test_update_add(self):
        IType_Major.create(self.major.pk, [self.itype_01.pk, self.itype_02.pk])
        item_list = IType_Major.objects.filter(major_id = self.major.pk)
        self.dele_list.extend([item.pk for item in item_list.iterator()])
        IType_Major.update(self.major.pk, self.itype_dele_list)
        item_list2 = IType_Major.objects.filter(major_id = self.major.pk)
        self.dele_list.extend([item.pk for item in item_list2.iterator()])
        self.dele_list = list(set(self.dele_list))
        self.assertEqual(item_list2.count(), len(self.itype_dele_list))
        for item in item_list2.iterator():
            self.assertIn(item.itype_id, self.itype_dele_list)
    def tet_update_reduce(self):
        item_list = IType_Major.create(self.major.pk, self.itype_dele_list)
        self.dele_list.extend([item.pk for item in item_list.iterator()])
        reduce_list = [self.itype_01.pk, self.itype_02.pk]
        item_list2 = IType_Major.update(self.major.pk, reduce_list)
        self.dele_list.extend([item.pk for item in item_list2.iterator()])
        self.assertEqual(item_list2.count(), len(reduce_list))
        for item in item_list2.iterator():
            self.assertIn(item.pk, reduce_list)

from pymongo import MongoClient
from pythonems.settings import MONGO_DATABASES
class Detail_Test(TestCase):
    def create_example(self):
        kwargs = {
            "id": 0,
            'body': "question body",
            'image': "/static/image/1/",
            "answer": "questin answer",
            "analysis": "questin analysis",
        }
        self.item_pk = Detail.create(kwargs)
        self.del_id.append(self.item_pk)
        self.example = self.collection.find_one({"_id": self.item_pk})
    @classmethod
    def setUpClass(cls):
        cls.collection = Detail.get_collection()
        cls.del_id = []
    def tearDown(self):
        self.collection.remove({"_id": {"$in": self.del_id}})
        self.del_id = []
    def test_create_success(self):
        self.create_example()
        item = self.example
        self.assertEqual(item["_id"], self.item_pk)
        self.assertEqual(item["id"], 0)
        self.assertEqual(item["body"], "question body")
        self.assertEqual(item["image"], "/static/image/1/")
        self.assertEqual(item["answer"], "questin answer")
        self.assertEqual(item["analysis"], "questin analysis")
    def test_creats_default(self):
        kwargs = {
            "id": 0
        }
        item_pk = Detail.create(kwargs)
        self.del_id.append(item_pk)
        item = self.collection.find_one({"_id": item_pk})
        self.assertEqual(item["body"], "")
        self.assertEqual(item["image"], "")
        self.assertEqual(item["answer"], "")
        self.assertEqual(item["analysis"], "")
    def test_create_exist_id(self):
        self.create_example()
        kwargs = {
            "id": self.item_pk,
        }
        self.assertRaises(Exception, Detail.create, (kwargs))
    def test_create_required_fields(self):
        kwargs = {
            'body': "question body",
            'image': "/static/image/1/",
            "answer": "questin answer",
            "analysis": "questin analysis",
        }
        self.assertRaises(Exception, Detail.create, (kwargs))
    def test_create_not_valid(self):
        kwargs = {
            'body': 1
        }
        self.assertRaises(Exception, Detail.create, (kwargs))
    def test_update_objectid(self):
        self.create_example()
        kwargs = {
            'body': "another question body, another",
            'image': "/static/image/2/",
            "answer": "another questin answer, another",
            "analysis": "another questin analysis, another",
        }
        result = Detail.update(self.example["_id"], kwargs)
        self.example = self.collection.find_one({"_id": self.item_pk})
        item = self.example
        self.assertEqual(item["_id"], self.item_pk)
        self.assertEqual(item["id"], 0)
        self.assertEqual(item["body"], "another question body, another")
        self.assertEqual(item["image"], "/static/image/2/")
        self.assertEqual(item["answer"], "another questin answer, another")
        self.assertEqual(item["analysis"], "another questin analysis, another")
    def test_update_id(self):
        self.create_example()
        kwargs = {
            'body': "another question body, another",
            'image': "/static/image/2/",
            "answer": "another questin answer, another",
            "analysis": "another questin analysis, another",
        }
        result = Detail.update(self.example["id"], kwargs)
        self.example = self.collection.find_one({"_id": self.item_pk})
        item = self.example
        self.assertEqual(item["_id"], self.item_pk)
        self.assertEqual(item["id"], 0)
        self.assertEqual(item["body"], "another question body, another")
        self.assertEqual(item["image"], "/static/image/2/")
        self.assertEqual(item["answer"], "another questin answer, another")
        self.assertEqual(item["analysis"], "another questin analysis, another")
    def test_update_single(self):
        self.create_example()
        kwargs = {
            'body': "another question body, another",
        }
        result = Detail.update(self.example["_id"], kwargs)
        self.example = self.collection.find_one({"_id": self.item_pk})
        item = self.example
        self.assertEqual(item["_id"], self.item_pk)
        self.assertEqual(item["id"], 0)
        self.assertEqual(item["body"], "another question body, another")
        self.assertEqual(item["image"], "/static/image/1/")
        self.assertEqual(item["answer"], "questin answer")
        self.assertEqual(item["analysis"], "questin analysis")
    def test_update_not_exist(self):
        self.create_example()
        kwargs = {
            'body': "another question body, another",
        }
        result = Detail.update(-1, kwargs)
        self.assertEqual(result["updatedExisting"], False)
    def test_update_twice(self):
        self.create_example()
        self.create_example()
        kwargs = {
            'body': "another question body, another",
        }
        result = Detail.update(self.example["id"], kwargs)
        item1, item2 = self.collection.find({"id": self.example["id"]})
        item = item1
        self.assertEqual(item["id"], 0)
        self.assertEqual(item["body"], "another question body, another")
        self.assertEqual(item["image"], "/static/image/1/")
        self.assertEqual(item["answer"], "questin answer")
        self.assertEqual(item["analysis"], "questin analysis")
        #only the first item is updated as default
        item = item2
        self.assertEqual(item["id"], 0)
        self.assertEqual(item["body"], "question body")
        self.assertEqual(item["image"], "/static/image/1/")
        self.assertEqual(item["answer"], "questin answer")
        self.assertEqual(item["analysis"], "questin analysis")
    def test_search_objectid(self):
        self.create_example()
        self.create_example()
        result = Detail.search(self.del_id)
        self.assertEqual(result.count(), 2)
    def test_search_id(self):
        self.create_example()
        self.create_example()
        result = Detail.search([0])
        self.assertEqual(result.count(), 2)
    def test_search_not_exist(self):
        self.create_example()
        self.create_example()
        result = Detail.search([-1])
        self.assertEqual(result.count(), 0)
        
        